{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "logging.basicConfig(level=logging.ERROR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepal length (cm)</th>\n",
       "      <th>sepal width (cm)</th>\n",
       "      <th>petal length (cm)</th>\n",
       "      <th>petal width (cm)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)\n",
       "0                5.1               3.5                1.4               0.2\n",
       "1                4.9               3.0                1.4               0.2\n",
       "2                4.7               3.2                1.3               0.2"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X, y = load_iris(return_X_y=True, as_frame=True)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "X[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    0\n",
       "2    0\n",
       "Name: target, dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hypergbm.estimators import XGBoostEstimator\n",
    "from hypergbm.pipeline import Pipeline, DataFrameMapper\n",
    "from hypergbm.sklearn.transformers import MinMaxScaler, StandardScaler\n",
    "from hypernets.core import OptimizeDirection\n",
    "from hypernets.core.ops import ModuleChoice, HyperInput\n",
    "from hypernets.core.search_space import HyperSpace\n",
    "from hypernets.tabular.column_selector import column_number_exclude_timedelta\n",
    "\n",
    "\n",
    "def search_space():\n",
    "    space = HyperSpace()\n",
    "    with space.as_default():\n",
    "        input = HyperInput(name='input1')\n",
    "        scaler_choice = ModuleChoice(\n",
    "            [\n",
    "                StandardScaler(name=f'numeric_standard_scaler'),\n",
    "                MinMaxScaler(name=f'numeric_minmax_scaler')\n",
    "            ], name=f'numeric_or_scaler'\n",
    "        )\n",
    "        num_pipeline = Pipeline([scaler_choice], name='numeric_pipeline', columns=column_number_exclude_timedelta)(input)\n",
    "        union_pipeline = DataFrameMapper(default=None, input_df=True, df_out=True)([num_pipeline])\n",
    "        xgb_est = XGBoostEstimator(fit_kwargs={})\n",
    "        ModuleChoice([xgb_est], name='estimator_options')(union_pipeline)  # Make xgboost as a estimator choice\n",
    "        space.set_inputs(input)\n",
    "    return space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:19:57 I hypernets.m.hyper_model.py 249 - 3 class detected, inferred as a [multiclass classification] task\n",
      "17:19:57 I hypernets.c.meta_learner.py 22 - Initialize Meta Learner: dataset_id:74e7c134740a0f846f4c5e57fa5e6c93\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>trial No.</th>\n",
       "      <th>Previous reward</th>\n",
       "      <th>Best trial</th>\n",
       "      <th>Best reward</th>\n",
       "      <th>Total elapsed</th>\n",
       "      <th>Max trials</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.176803</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   trial No.  Previous reward  Best trial  Best reward  Total elapsed  \\\n",
       "0          2              1.0           1          1.0       0.176803   \n",
       "\n",
       "   Max trials  \n",
       "0          10  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "#### Current Trial:"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Trial No.</th>\n",
       "      <th>Reward</th>\n",
       "      <th>Elapsed</th>\n",
       "      <th>Space Vector</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.122744</td>\n",
       "      <td>[1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.090189</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Trial No.  Reward   Elapsed Space Vector\n",
       "0          1     1.0  0.122744          [1]\n",
       "1          2     1.0  0.090189          [0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Pass classes=[0 1 2], y=22     0\n",
      "15     0\n",
      "65     1\n",
      "11     0\n",
      "42     0\n",
      "      ..\n",
      "71     1\n",
      "106    2\n",
      "14     0\n",
      "92     1\n",
      "102    2\n",
      "Name: target, Length: 120, dtype: int64 as keyword args. From version 0.25 passing these as positional arguments will result in an error\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "#### Best Trial:"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "<thead>\n",
       "<tr style=\"text-align: right;\">\n",
       "  <th>key</th>\n",
       "  <th>value</th>\n",
       "</tr>\n",
       "</thead>\n",
       "<tbody><tr>\n",
       "  <td>signature</td>\n",
       "  <td>8a42827ddc672747f568cdd0f9f46c2d</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <td>vectors</td>\n",
       "  <td>[1]</td>\n",
       "</tr><tr>\n",
       "  <td>0-numeric_or_scaler.hp_or</td>\n",
       "  <td>1</td>\n",
       "</tr>\n",
       "<tr>  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "DAG_HyperSpace_1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:19:57 I hypernets.d.in_process_dispatcher.py 119 - Trial 1 done, reward: 1.0, best_trial_no:1, best_reward:1.0\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Pass classes=[0 1 2], y=22     0\n",
      "15     0\n",
      "65     1\n",
      "11     0\n",
      "42     0\n",
      "      ..\n",
      "71     1\n",
      "106    2\n",
      "14     0\n",
      "92     1\n",
      "102    2\n",
      "Name: target, Length: 120, dtype: int64 as keyword args. From version 0.25 passing these as positional arguments will result in an error\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:19:57 I hypernets.d.in_process_dispatcher.py 119 - Trial 2 done, reward: 1.0, best_trial_no:1, best_reward:1.0\n",
      "\n",
      "17:20:08 I hypernets.d.in_process_dispatcher.py 41 - Unable to take valid sample and exceed the retry limit 1000.\n"
     ]
    }
   ],
   "source": [
    "from hypergbm import HyperGBM\n",
    "from hypernets.searchers import MCTSSearcher\n",
    "rs = MCTSSearcher(search_space, max_node_space=10, optimize_direction=OptimizeDirection.Maximize)\n",
    "hk = HyperGBM(rs, task='multiclass', reward_metric='accuracy', callbacks=[])\n",
    "hk.search(X_train, y_train, X_eval=X_test, y_eval=y_test)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
